package com.backend.modules.system.controller;

import com.backend.common.model.R;
import com.backend.modules.system.convert.DepartmentConvert;
import com.backend.modules.system.convert.TeacherConvert;
import com.backend.modules.system.dto.TeacherDTO;
import com.backend.modules.system.dto.TeacherQueryDTO;
import com.backend.modules.system.service.TeacherService;
import com.backend.modules.system.vo.TeacherClassVO;
import com.backend.modules.system.vo.TeacherVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "教师管理", description = "教师管理相关接口")
@RestController
@RequestMapping("/v1/teachers")
@RequiredArgsConstructor
public class TeacherController {

    private final TeacherService teacherService;

    @Operation(summary = "创建教师")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> create(@Validated @RequestBody TeacherDTO dto) {
        teacherService.create(dto);
        return R.ok();
    }

    @Operation(summary = "更新教师")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody TeacherDTO dto) {
        teacherService.update(id, dto);
        return R.ok();
    }

    @Operation(summary = "获取教师详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<TeacherVO> getById(@PathVariable Long id) {
        TeacherVO vo = TeacherConvert.convert(teacherService.getById(id));
        // 设置负责班级列表
        List<TeacherClassVO> classes = DepartmentConvert.convertClassList(
                teacherService.getTeacherClasses(id));
        vo.setClasses(classes);
        return R.ok(vo);
    }

    @Operation(summary = "分页查询教师")
    @GetMapping("/page")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Page<TeacherVO>> page(
            @Schema(description = "工号") @RequestParam(required = false) String userCode,
            @Schema(description = "姓名") @RequestParam(required = false) String realName,
            @Schema(description = "手机号") @RequestParam(required = false) String phone,
            @Schema(description = "负责班级ID列表") @RequestParam(required = false) List<Long> classIds,
            @ParameterObject Pageable pageable) {
        TeacherQueryDTO query = new TeacherQueryDTO();
        query.setUserCode(userCode);
        query.setRealName(realName);
        query.setPhone(phone);
        query.setClassIds(classIds);
        
        Page<TeacherVO> page = teacherService.page(query, pageable)
                .map(teacher -> {
                    TeacherVO vo = TeacherConvert.convert(teacher);
                    // 设置负责班级列表
                    List<TeacherClassVO> classes = DepartmentConvert.convertClassList(
                            teacherService.getTeacherClasses(teacher.getId()));
                    vo.setClasses(classes);
                    return vo;
                });
        
        return R.ok(page);
    }

    @Operation(summary = "获取可选班级列表")
    @GetMapping("/available-classes")
    @PreAuthorize("hasRole('ADMIN')")
    public R<List<TeacherClassVO>> getAvailableClasses(
            @Parameter(description = "专业ID") @RequestParam(required = false) Long majorId) {
        return R.ok(DepartmentConvert.convertClassList(
                teacherService.getAvailableClasses(majorId)));
    }

    @Operation(summary = "删除教师")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> delete(@PathVariable Long id) {
        teacherService.delete(id);
        return R.ok();
    }

    @Operation(summary = "更新教师状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> updateStatus(
            @PathVariable Long id,
            @Schema(description = "状态") @RequestParam Boolean status) {
        teacherService.updateStatus(id, status);
        return R.ok();
    }

    @Operation(summary = "重置教师密码")
    @PutMapping("/{id}/password/reset")
    @PreAuthorize("hasRole('ADMIN')")
    public R<Void> resetPassword(@PathVariable Long id) {
        teacherService.resetPassword(id);
        return R.ok();
    }
} 